package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.ResultSetMapper;
import util.jdbcmysql;
import vo.Matcher;

public class MatcherInfoDao {

	private Connection con = null; 
	private Statement stat = null; 
	private ResultSet rs = null; 
	private PreparedStatement pst = null; 	
	
	private String tableName ="matcher_info";
	
	public MatcherInfoDao()
	{
		con = new jdbcmysql().getConnection();
	}
	
	public boolean updateMatcherInfo(Matcher m)
	{
		boolean res = false;
		String sqlStr = "update "+tableName+" set residence='"+m.getResidence()+"' ,height_Low="+m.getHeightLow()+" ,height_High="+m.getHeightHigh();
		sqlStr+=" where member_ID = '"+m.getMemberID()+"'";
	    try 
	    { 
	      stat = con.createStatement(); 
	      int check = stat.executeUpdate(sqlStr); 
	      System.out.println("selectSQL:"+sqlStr);	
	      
	      if(check !=0)
	      {
	    	  res = true;
	      }
	      
	    } 
	    catch(SQLException e) 
	    { 
	      System.out.println("updateMatcherInfo DropDB Exception :" + e.toString()); 
	    } 
	    finally 
	    { 
	      Close(); 
	    }	
	    return res;
	}
	
	public Matcher searchByID(String memberID)
	{
		List<Matcher> pojoList = new ArrayList<Matcher>();
		ResultSetMapper<Matcher> resultSetMapper = new ResultSetMapper<Matcher>();
		String sqlStr = "select * from "+tableName+" as m where m.member_ID = '"+memberID+"' ";
	    try 
	    { 
	      stat = con.createStatement(); 
	      rs = stat.executeQuery(sqlStr); 
	      System.out.println("selectSQL:"+sqlStr);	
	      
	      pojoList= resultSetMapper.mapRersultSetToObject(rs, Matcher.class);
	      
	    } 
	    catch(SQLException e) 
	    { 
	      System.out.println("searchMemberInfoByList DropDB Exception :" + e.toString()); 
	    } 
	    finally 
	    { 
	      Close(); 
	    }	
	    return pojoList.get(0);
		
	} 
	
	private void Close() 
	{ 
	  try 
	  { 
	    if(rs!=null) 
	    { 
	      rs.close(); 
	      rs = null; 
	    } 
	    if(stat!=null) 
	    { 
	      stat.close(); 
	      stat = null; 
	    } 
	    if(pst!=null) 
	    { 
	      pst.close(); 
	      pst = null; 
	    } 
	  } 
	 catch(SQLException e) 
	  { 
	    System.out.println("Close Exception :" + e.toString()); 
	  } 
	}
	
}
